package com.xjtu.conan.controller;


import com.xjtu.conan.entity.TheatreCommentsInfo;
import com.xjtu.conan.entity.TheatreInfo;
import com.xjtu.conan.entity.UserInfo;
import com.xjtu.conan.response.AjaxResponse;
import com.xjtu.conan.service.TheatreCommentsInfoService;
import com.xjtu.conan.service.TheatreInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * <p>
 * 剧场版评论表 前端控制器
 * </p>
 *
 * @author 一只黑猩猩
 * @since 2021-04-19
 */
@CrossOrigin
@RestController
@RequestMapping("/conan/theatre-comments-info")
@Api(value = "剧场版对应评论管理模块", description = "这是剧场版对应评论信息CRUD的模块")
public class TheatreCommentsInfoController {
    @Autowired
    private TheatreCommentsInfoService commentsInfoService;

    @ApiOperation(value = "获取全部剧场版评论", notes = "获取所有剧场版的评论信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页", required = false, defaultValue = "1", paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "size", value = "每页数量", required = false, defaultValue = "10", paramType = "query", dataType = "int")
    })
    @RequestMapping(value = "/getAllComments", method = RequestMethod.GET)
    public AjaxResponse getAllComments(@RequestParam(value = "current") Integer current,
                                       @RequestParam(value = "size") Integer size) {

        return AjaxResponse.success(commentsInfoService.getAllTheatreCommentsInfo(current, size));
    }

    @ApiOperation(value = "添加剧场版评论", notes = "为某一个剧场版添加评论")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "content", value = "评论内容", required = true, paramType = "form", dataType = "String"),
            @ApiImplicitParam(name = "tid", value = "所属剧场版id", required = true, paramType = "form", dataType = "Long"),
            @ApiImplicitParam(name = "pid", value = "所属父评论id", required = true, defaultValue = "-1", paramType = "form", dataType = "Long")
    })
    @RequestMapping(value = "addTheatreComments", method = RequestMethod.POST)
    public AjaxResponse addTheatreComments(@RequestParam(value = "content") String content,
                                           @RequestParam(value = "tid") Long tid,
                                           @RequestParam(value = "pid") Long pid,
                                           HttpServletRequest request) throws IOException {
        UserInfo loginUser = (UserInfo) request.getSession().getAttribute("loginUser");
        TheatreCommentsInfo commentsInfo = new TheatreCommentsInfo()
                .setTheatreId(tid)
//                .setUserId(loginUser.getUid())
                .setUserId(1384414001958338562L)
                .setTcContent(content)
                .setParentId(pid);
        return AjaxResponse.success("新增评论ID->" + commentsInfoService.addTheatreComments(commentsInfo));
    }

    @ApiOperation(value = "更新剧场版评论", notes = "录入新的剧场版评论信息并且修改")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "content", value = "评论内容", required = true, paramType = "form", dataType = "String"),
            @ApiImplicitParam(name = "tid", value = "所属剧场版id", required = true, paramType = "form", dataType = "Long"),
            @ApiImplicitParam(name = "pid", value = "所属父评论id", required = true, defaultValue = "-1", paramType = "form", dataType = "Long")
    })
    @RequestMapping(value = "updateTheatreComments", method = RequestMethod.PUT)
    public AjaxResponse updateTheatreComments(@RequestParam(value = "content") String content,
                                              @RequestParam(value = "tid") Long tid,
                                              @RequestParam(value = "pid") Long pid,
                                              HttpServletRequest request) throws IOException {
        UserInfo loginUser = (UserInfo) request.getSession().getAttribute("loginUser");
        TheatreCommentsInfo commentsInfo = new TheatreCommentsInfo()
                .setTheatreId(tid)
//                .setUserId(loginUser.getUid())
                .setUserId(1384414001958338562L)
                .setTcContent(content)
                .setParentId(pid);
        return AjaxResponse.success(commentsInfoService.updateTheatreComments(commentsInfo));
    }

    @ApiOperation(value = "删除剧场版评论信息", notes = "从数据库中删除某个剧场版")
    @ApiImplicitParam(name = "tcid", value = "删除剧场版id", required = true, paramType = "query", dataType = "Long")
    @RequestMapping(value = "/deleteTheatreComments", method = RequestMethod.DELETE)
    public AjaxResponse deleteTheatreComments(@RequestParam(value = "tcid") Long tcid) {
        return AjaxResponse.success("数据库受影响行数->" + commentsInfoService.deleteTheatreComments(tcid));
    }

    @ApiOperation(value = "根据父评论id查找剧场版评论", notes = "获取根据父评论id查找到的剧场版评论")
    @ApiImplicitParam(name = "pid", value = "父评论id", required = true, paramType = "query", dataType = "Long")
    @RequestMapping(value = "/getTheatreCommentsBypId", method = RequestMethod.GET)
    public AjaxResponse getTheatreCommentsBypId(@RequestParam(name = "pid") Long pid) {
        return AjaxResponse.success(commentsInfoService.getTheatreCommentsBypid(pid));
    }

    @ApiOperation(value = "根据剧场版评论id查找剧场版评论", notes = "获取根据评论id查找到的剧场版评论")
    @ApiImplicitParam(name = "tcid", value = "评论id", required = true, paramType = "query", dataType = "Long")
    @RequestMapping(value = "/getTheatreCommentsBytcId", method = RequestMethod.GET)
    public AjaxResponse getTheatreCommentsBytcId(@RequestParam(name = "tcid") Long tcid) {
        return AjaxResponse.success(commentsInfoService.getTheatreCommentByid(tcid));
    }

    @ApiOperation(value = "根据剧场版id查找对应评论", notes = "获取根据剧场版id查找到的剧场版评论")
    @ApiImplicitParam(name = "tid", value = "剧场版id", required = true, paramType = "query", dataType = "Long")
    @RequestMapping(value = "/getTheatreCommentsBytid", method = RequestMethod.GET)
    public AjaxResponse getTheatreCommentsBytid(@RequestParam(name = "tid") Long tid) {
        return AjaxResponse.success(commentsInfoService.getTheatreCommentsBytid(tid));
    }

    @ApiOperation(value = "剧场版评论总数量", notes = "获取数据库中剧场版评论总数量")
    @RequestMapping(value = "/getTotal", method = RequestMethod.GET)
    public AjaxResponse getTotal() {
        return AjaxResponse.success(commentsInfoService.getTotal());
    }
}

